package com.example.gulimall.product.app;

import java.util.Arrays;
import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.service.AttrAttrgroupRelationService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.service.CategoryService;
import com.example.gulimall.product.vo.AttrGroupRelationVo;
import com.example.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 属性分组
 *
 * @author xiaoming
 * @email 434216555@qq.com
 * @date 2021-06-21 19:51:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long[] catelogPath=categoryService.findCateLogPathById(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    /**
     * 查询分类分组下的所有属性
     */
    @GetMapping("{attrgroupId}/attr/relation")
    public R getAttrsInfo(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> entities=attrGroupService.getAttrsInfo(attrGroupId);
        return R.ok().put("data",entities);
    }
    /**
     * 删除关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(AttrGroupRelationVo[] relationVos){
        attrAttrgroupRelationService.deleteRelation(relationVos);
        return R.ok();
    }
    //product/attrgroup/{attrgroupId}/noattr/relation
    /**
     * 查询属性分组中未被关联的属性
     */
    @PostMapping("/{attrgroupId}/noattr/relation")
    public R otherAttrs(@RequestParam Map<String, Object> params,
                        @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page=attrService.getOtherAttrs(params,attrgroupId);
        return R.ok().put("page",page);
    }
    //product/attrgroup/{catelogId}/withattr
    /**
     * 添加属性分组的关联关系
     */
    @PostMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos
                =attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVos);
    }
    //product/attrgroup/attr/relation
    /**
     * 添加属性分组的关联关系
     */
    @GetMapping("/attr/relation")
    public R setRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrAttrgroupRelationService.setRelation(attrGroupRelationVos);
        return R.ok();
    }


}
